/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

import javax.microedition.lcdui.*;
import java.util.*;
import sUtils.*;



/**
 *
 * @author thomas.s
 */
public class TextImage {

    Vector v=null;
    int txtColor = 0, bgColor=0, h=60; long start=0;
    Image img = null;
    public TextImage( Vector entries, long t, int ch, int bg, int txt ) {
        bgColor = bg;
        txtColor = txt;
        start = t;
        h=(ch-4)*2;
        v=entries;
        updateImage();
    }
    int colors[] = { 0, 0xc0c000, 0xc08000, 0xc08000, 0xc08000, 0x2080c0 };

    public int drawText( Graphics g, int max, int col, Font medium, Font small )
    {
        int ret = 0;
        if (v!=null) {
            int lineA=0, lineB=0, lineC=0, space=20, tw;
            int count=v.size();

            for (int i=0; i<count; i++ ) {
                MyDateEntry e = (MyDateEntry)v.elementAt(i);
                String s = e.getDesc(start);
                tw = medium.stringWidth(s);
                if (e.type>0) col = colors[e.type];
                if (lineB==0 &&((((tw+lineA+space) < max)/*&&(count>3)*/) || lineA==0))
                {
                    g.setFont(medium);
                    if (lineA>0) { g.drawString("; ", lineA, 0, 0); lineA+=space; }
                    g.setColor( col );
                    g.drawString(s, lineA, 0, 0);
                    lineA+=tw;
                    continue;
                }
                tw = small.stringWidth(s);
                if (lineC==0 && ((((tw+lineB+space) < max)/*&&(count>3)*/) || lineB==0))
                {
                    g.setFont(small);
                    if (lineB>0) { g.drawString("; ", lineB, 0, 0); lineB+=space; }
                    g.setColor( col );
                    g.drawString(s, lineB, 34, 0);
                    lineB+=tw;
                    continue;
                }
                if ((((tw+lineC+space) < max)/*&&(count>3)*/) || lineC==0)
                {
                    g.setFont(small);
                    if (lineC>0) { g.drawString("; ", lineC, 0, 0); lineC+=space; }
                    g.setColor( col );
                    g.drawString(s, lineC, 60, 0);
                    lineC+=tw;
                }
            }

            if (lineA>ret) ret=lineA;
            if (lineB>ret) ret=lineB;
            if (lineC>ret) ret=lineC;
        }
        return ret;
    }

    public void updateImage()
    {
        img = null;
        if (v==null) return;
        Font medium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        Font small = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);

        int w = (480-110)*2;
        Image line = Image.createImage(w,h);
        Graphics g = line.getGraphics();
        g.setColor(bgColor);
        g.fillRect(0,0,w,h);
        g.setColor(txtColor);
        int tw = drawText(g,w,txtColor,medium,small);

        img = Images.scaleDownKeyed( line, bgColor);
    }

    public void draw( Graphics g, int x, int y )
    {
        if (img!=null) g.drawImage(img,x,y,0);
    }
}
